package com.yeah.sinaspider.controller;

import com.yeah.sinaspider.service.WeiboService;
import com.yeah.sinaspider.utils.DriverUtil;
import com.yeah.sinaspider.vo.LoginParam;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @FileName TestController.java
 * @Description TODO
 * @Author yeah、一页
 * @Email 1324459373@qq.com|quhailong1995@gmail.com
 * @Date 2020/4/18 17:30
 * @Version 1.0
 */
@RestController
@RequestMapping("/weibo/pt")
public class WeiboPtController {

    @Autowired
    private WeiboService weiboService;

    @PostMapping("loginByAccount/{driverKey}")
    public ModelAndView loginByAccount(@ModelAttribute(value = "loginParam")LoginParam loginParam,@PathVariable("driverKey") String driverKey) throws Exception {

        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");

        Map<String,Object> res = new HashMap<>();
        res.put("status",200);

        RemoteWebDriver driver = null;
        try {
            driver = weiboService.loginByAccount(loginParam, DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey),driverKey);
            res.put("img","data:image/png;base64,"+driver.getScreenshotAs(OutputType.BASE64));
        } catch (Exception e) {
            e.printStackTrace();
            res.put("status",400);
            res.put("msg",e.getMessage());
        }

        ModelAndView modelAndView = new ModelAndView("login_pt");

        modelAndView.addObject("res",res);

        return modelAndView;
    }

    @GetMapping("loginByQR/{driverKey}")
    public ModelAndView loginByQR(@PathVariable("driverKey") String driverKey) throws Exception {

        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");

        Map<String,Object> res = new HashMap<>();
        res.put("status",200);

        RemoteWebDriver driver = null;
        try {
            driver = weiboService.loginByQR(DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey),driverKey);
            res.put("img","data:image/png;base64,"+driver.getScreenshotAs(OutputType.BASE64));
        } catch (Exception e) {
            e.printStackTrace();
            res.put("status",400);
            res.put("msg",e.getMessage());
        }

        ModelAndView modelAndView = new ModelAndView("login_pt_qr");

        modelAndView.addObject("res",res);

        return modelAndView;
    }

    @GetMapping("index/{driverKey}")
    public String index(@PathVariable("driverKey") String driverKey,String href) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        //RemoteWebDriver driver = DriverUtil.getPhantomJSDriver();
        RemoteWebDriver driver = DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey);
        driver.get(href);
        return driver.getPageSource();
    }

    @GetMapping("loginPage/{driverKey}")
    public ModelAndView loginPage(@ModelAttribute(value = "loginParam")LoginParam loginParam,@PathVariable("driverKey") String driverKey) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        Map<String,Object> res = new HashMap<>();
        res.put("status","200");
        ModelAndView modelAndView = new ModelAndView("login_pt");
        modelAndView.addObject("res",res);
        modelAndView.addObject("driverKey",driverKey);
        return modelAndView;
    }

    @GetMapping("loginQRPage/{driverKey}")
    public ModelAndView loginQRPage(@PathVariable("driverKey") String driverKey) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        Map<String,Object> res = new HashMap<>();
        res.put("status","200");
        ModelAndView modelAndView = new ModelAndView("login_pt_qr");
        modelAndView.addObject("res",res);
        modelAndView.addObject("driverKey",driverKey);
        return modelAndView;
    }


    @GetMapping("publishComment/{driverKey}")
    public Map<String, Object> publishComment(@PathVariable("driverKey") String driverKey) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        Map<String,Object> res = new HashMap<>();
        res.put("status","200");
        res.put("msg","success");
        try {
            weiboService.execAutoComment(DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey),driverKey);
        }catch (Exception e){
            res.put("status","400");
            res.put("msg",e.getMessage());
        }
        return res;
    }


    @GetMapping("start")
    public ModelAndView start(String driverKey) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey);
        ModelAndView modelAndView = new ModelAndView("main_pt");
        modelAndView.addObject("driverKey",driverKey);
        return modelAndView;
    }

    @GetMapping("stop/{driverKey}")
    public String stop(@PathVariable("driverKey") String driverKey) throws Exception {
        if(!StringUtils.hasText(driverKey))throw new Exception("driverKey not allow empty");
        DriverUtil.getPhantomJSDriverFromDriverMapByKey(driverKey).quit();
        DriverUtil.removePhantomJSDriver(driverKey);
        return "操作成功";
    }


}